package com.yunfeng.supercall.fragment;

import java.util.HashMap;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.utils.UIHandler;

import com.lin.utils.Utils;
import com.ly.quickdev.library.activity.DevBaseActivity;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.AboutUsActivity;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.FriendShareListActivity;
import com.yunfeng.supercall.activity.MainActivity;
import com.yunfeng.supercall.activity.PersonalDetailActivity;
import com.yunfeng.supercall.activity.SettingActivity;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.UserBean;
import com.yunfeng.supercall.iface.MessageAccessable;
import com.yunfeng.supercall.manager.CommentCountManager;
import com.yunfeng.supercall.manager.ShareManager;

public class MeFragment extends BaseFragment implements MessageAccessable, Callback, PlatformActionListener {

	private UserBean mUser;
	private TextView mTextViewCommentCount;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mUser = BaseActivity.getUser(getActivity());
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragmenti_mine;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mTextViewCommentCount = findViewById(R.id.comment_count);
		view.findViewById(R.id.my_share).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						startActivity(new Intent(getActivity(),
								FriendShareListActivity.class).putExtra(
								KEY_DATA, mUser.getUserid()));
					}
				});
		view.findViewById(R.id.setting).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						startActivity(new Intent(getActivity(),
								SettingActivity.class));
					}
				});
		view.findViewById(R.id.about).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), AboutUsActivity.class));
			}
		});
		view.findViewById(R.id.user_area).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						startActivity(new Intent(getActivity(),
								PersonalDetailActivity.class));
					}
				});
		view.findViewById(R.id.share_area).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						View content = getActivity().getLayoutInflater()
								.inflate(R.layout.dialog_share, null);
						Dialog mDialog = DevBaseActivity.getDialog(
								getActivity(), content, null);
						setUpImage(content);
					}
				});
		refreshMessage();
	}

	private void setUpImage(View v) {
		if (v instanceof ImageView) {
			v.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int img = v.getId();
					if (img == R.id.weibo) {
						ShareManager.shareToWeiBo(getActivity());
					} else if (img == R.id.wechat_circle) {
						int userid = BaseActivity.getUser(getActivity()).getUserid();
						//Utils.shareWechatCircle(getActivity(), MeFragment.this, userid);
					} else if (img == R.id.wechat_friends) {
						int userid = BaseActivity.getUser(getActivity()).getUserid();
						//Utils.shareWechatToFriends(getActivity(), MeFragment.this, userid);
					} else if (img == R.id.qq) {
						ShareManager.shareToQQ(getActivity());
					} else if (img == R.id.sms_share){
						int userid = BaseActivity.getUser(getActivity()).getUserid();
						Utils.sendSMS(getActivity(), getResources().getString(R.string.sms_share, userid));
					}
				}
			});
		} else if (v instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) v).getChildCount(); i++) {
				setUpImage(((ViewGroup) v).getChildAt(i));
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		mUser.setupUserView(getView());
		AppContext.displayImgWithDefault1(
				(ImageView) findViewById(R.id.avatar), mUser.getPhoto());
	}

	@Override
	public void refreshMessage() {
		if (mTextViewCommentCount == null) {
			return;
		}
		int old = CommentCountManager.getInstance(getActivity()).getCurrentId();
		int count = ((MainActivity) getActivity()).mCommentCount;
		if (count == 0 || old == 0) {
			mTextViewCommentCount.setVisibility(View.INVISIBLE);
		} else {
			mTextViewCommentCount.setVisibility(View.VISIBLE);
		}
		mTextViewCommentCount.setText(count + "");
	}

    @Override
    public boolean handleMessage(Message msg) {
        int what = msg.what;
        if (what == 1) {
            Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

	@Override
    public void onCancel(Platform arg0, int arg1) {
        Message msg = new Message();
        msg.what = 0;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onComplete(Platform plat, int action,
            HashMap<String, Object> res) {
        Message msg = new Message();
        msg.arg1 = 1;
        msg.arg2 = action;
        msg.obj = plat;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onError(Platform arg0, int arg1, Throwable arg2) {
        Message msg = new Message();
        msg.what = 1;
        UIHandler.sendMessage(msg, this);
    }

}
